package org.jeecg.modules.pay.timerHander;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import groovy.util.logging.Slf4j;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.common.base.BaseMap;
import org.jeecg.modules.pay.entity.Resources;
import org.jeecg.modules.pay.entity.ResourcesUsage;
import org.jeecg.modules.pay.entity.UserAccount;
import org.jeecg.modules.pay.service.IAccountChangeService;
import org.jeecg.modules.pay.service.IResourcesService;
import org.jeecg.modules.pay.service.IResourcesUsageService;
import org.jeecg.modules.pay.service.IUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author OSFung
 * @date 2022-06-15 06:06
 */
@Component
@Slf4j
public class DeductionJobHandler {

    private IResourcesService resourcesService;

    private IResourcesUsageService resourcesUsageService;

    private RabbitMqClient rabbitMqClient;

    private IAccountChangeService accountChangeService;

    private IUserAccountService userAccountService;

    @Autowired
    public DeductionJobHandler(IResourcesService resourcesService, IResourcesUsageService resourcesUsageService,
                               RabbitMqClient rabbitMqClient, IAccountChangeService accountChangeService, IUserAccountService userAccountService) {
        this.resourcesService = resourcesService;
        this.resourcesUsageService = resourcesUsageService;
        this.rabbitMqClient = rabbitMqClient;
        this.accountChangeService = accountChangeService;
        this.userAccountService = userAccountService;
    }

    @XxlJob(value = "Deduction")
    public ReturnT<String> DeductionJobHandler(String params) {
        Calendar cal= Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
        double de = year + (month * 0.01);
        BigDecimal bigDecimal = new BigDecimal(de);
        QueryWrapper<Resources> resourcesQueryWrapper = new QueryWrapper<>();
        resourcesQueryWrapper.eq("resources_date", bigDecimal);
        List<Resources> resources = resourcesService.list(resourcesQueryWrapper);
        for (Resources resource : resources) {
            List<ResourcesUsage> resourcesUsages = resourcesUsageService.listByIds(resources.stream().map(Resources::getId).collect(Collectors.toList()));
            resourcesUsages.stream().peek(x -> {
                // 发送消息队列进行充值消费记录
                BaseMap map = new BaseMap();
                BigDecimal changeNum = resource.getResourcesPrice().multiply(new BigDecimal(x.getUsages()));
                map.put("changeNum", changeNum);
                BigDecimal before = userAccountService.getById(x.getUserId()).getBalance();
                map.put("beforeChange", before);
                BigDecimal after = before.subtract(changeNum);
                userAccountService.updateById(new UserAccount().setId(x.getUserId()).setBalance(after));
                map.put("AfterChange", after);
                map.put("id", x.getUserId());
                rabbitMqClient.sendMessage("accountChange", map);
                x.setState("commited");
            }).forEach(resourcesUsageService::updateById);
        }
        return ReturnT.SUCCESS;
    }
}
